import api from "@/http/api";
import { CHECK_USER_TOKEN, REFRESH_USER_TOKEN, USER_LOGIN } from "@/http/uri";
import { defineStore } from "pinia";

export const useUserStore = defineStore({
  id: "user",
  state: () => ({
    token: localStorage.getItem("NIL_TOKEN") || "",
    info: {
      id: "",
      uid: "",
      email: "",
      head: "",
      name: "",
      sign: "",
      status: "",
      role: "",
      token: "",
    },
  }),
  getters: {
    getUserInfo: (state) => state.info,
    getToken: (state) => state.token,
  },
  actions: {
    setToken(token) {
      this.token = token;
      this.info.token = token;
      localStorage.setItem("NIL_TOKEN", token);
    },
    login(data) {
      return new Promise((resolve, reject) => {
        api
          .POST(USER_LOGIN, data)
          .then((res) => {
            if (res.success) {
              this.setToken(res.data);
              resolve(res);
            } else {
              reject(res.message);
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    checkToken() {
      return new Promise((resolve, reject) => {
        api
          .GET(CHECK_USER_TOKEN)
          .then((res) => {
            if (res.success) {
              this.token = res.data.token;
              this.info.token = res.data.token;
              this.info.id = res.data.id;
              this.info.uid = res.data.uid;
              this.info.email = res.data.email;
              this.info.head = res.data.head;
              this.info.name = res.data.name;
              this.info.sign = res.data.sign;
              this.info.status = res.data.status;
              this.info.role = res.data.role;
              localStorage.setItem("NIL_TOKEN", res.data.token);
              resolve(this.info);
            } else {
              reject(res.message);
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    refreshToken() {
      return new Promise((resolve, reject) => {
        api
          .GET(REFRESH_USER_TOKEN)
          .then((res) => {
            if (res.success) {
              this.token = res.data.token;
              this.info.token = res.data.token;
              this.info.id = res.data.id;
              this.info.uid = res.data.uid;
              this.info.email = res.data.email;
              this.info.head = res.data.head;
              this.info.name = res.data.name;
              this.info.sign = res.data.sign;
              this.info.status = res.data.status;
              this.info.role = res.data.role;
              localStorage.setItem("NIL_TOKEN", res.data.token);
              resolve(this.info);
            } else {
              reject(res.message);
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    logout() {
      return new Promise((resolve) => {
        localStorage.removeItem("NIL_TOKEN");
        this.token = "";
        this.info.token = "";
        this.info.id = "";
        this.info.uid = "";
        this.info.email = "";
        this.info.head = "";
        this.info.name = "";
        this.info.sign = "";
        this.info.status = "";
        this.info.role = "";
        resolve(this.token);
      });
    },
  },
});
